package com.ccjq.jq173.shosestore.member.dao.impl;

import com.ccjq.jq173.shosestore.core.db.manager.DbConnection;
import com.ccjq.jq173.shosestore.member.dao.UserDao;
import com.ccjq.jq173.shosestore.member.po.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {

    @Override
    public int save(String userId, String username, String password, String mobilePhone) {
        int result = 0;
        String sql = "insert into t_user(user_id,username,password,mobile_phone) values(?,?,?,?)";

        Connection con = null;
        PreparedStatement st = null;

        con = DbConnection.getConnection();
        try {
            st = con.prepareStatement(sql);
            st.setString(1, userId);
            st.setString(2, username);
            st.setString(3, password);
            st.setString(4, mobilePhone);
            result = st.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(null, st, con);
        }
        return result;
    }

    @Override
    public User findUserByUserName(String username) {
        User user = null;
        String sql = "select * from t_user where username=?";
        Connection con = null;
        PreparedStatement st = null;
        ResultSet rs = null;

        try {
            con = DbConnection.getConnection();
            st = con.prepareStatement(sql);
            st.setString(1, username);
            rs = st.executeQuery();
            if (rs.next()) {
                user = new User();
                user.setUserId(rs.getString(1));
                user.setUsername(rs.getString(2));
                user.setPassword(rs.getString(3));
                user.setMobilePhone(rs.getString(4));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(rs, st, con);
        }

        return user;
    }

    @Override
    public User findUserByNmaePassword(String username, String password)
    {
        User user = null;
        String sql = "select * from t_user where username=? and password=?";
        Connection con = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        try {
            con = DbConnection.getConnection();
            st = con.prepareStatement(sql);
            st.setString(1, username);
            st.setString(2, password);
            rs = st.executeQuery();
            if (rs.next()) {
                user = new User();
                user.setUserId(rs.getString(1));
                user.setUsername(rs.getString(2));
                user.setPassword(rs.getString(3));
                user.setMobilePhone(rs.getString(4));

            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            DbConnection.close(rs, st, con);
        }
        return user;
    }

    @Override
    public User findUserByUserId(String userId) {
        User user = null;
        String sql = "select * from t_user where user_id=?";
        Connection con = null;
        PreparedStatement st = null;
        ResultSet rs = null;

        try {
            con = DbConnection.getConnection();
            st = con.prepareStatement(sql);
            st.setString(1, userId);
            rs = st.executeQuery();
            if (rs.next()) {
                user = new User();
                user.setUserId(rs.getString(1));
                user.setUsername(rs.getString(2));
                user.setPassword(rs.getString(3));
                user.setMobilePhone(rs.getString(4));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(rs, st, con);
        }

        return user;
    }

}
